package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.LoginRequest;
import com.jjwxc.jwjskandriod.model.LoginResponse;

public class PasswordActivity extends BaseActivity {

    private EditText ed_password_one;
    private EditText ed_password_two;

    private ImageView img_show_one;
    private ImageView img_show_two;
    private TextView tv_ok;
    private LinearLayout ll_fanhui;

    private boolean oneShow = false;
    private boolean twoShow = false;

    @Override
    public int getContentViewId() {
        return R.layout.activity_password;
    }

    @Override
    public void findView() {
        setNoTitle();
        ed_password_one = findViewById(R.id.ed_password_one);
        ed_password_two = findViewById(R.id.ed_password_two);
        img_show_one = findViewById(R.id.img_show_one);
        img_show_two = findViewById(R.id.img_show_two);
        tv_ok = findViewById(R.id.tv_ok);
        ll_fanhui = findViewById(R.id.ll_fanhui);
    }

    @Override
    public void setListener() {
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_show_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oneShow) {
                    oneShow = false;
                    img_show_one.setImageResource(R.mipmap.mima_false);
                    ed_password_one.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    oneShow = true;
                    img_show_one.setImageResource(R.mipmap.mima_true);
                    ed_password_one.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
        img_show_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (twoShow) {
                    twoShow = false;
                    img_show_two.setImageResource(R.mipmap.mima_false);
                    ed_password_two.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    twoShow = true;
                    img_show_two.setImageResource(R.mipmap.mima_true);
                    ed_password_two.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
    }

    @Override
    public void afterCreate() {
        String nextCode = getIntent().getStringExtra("nextCode");
        String phone = getIntent().getStringExtra("phone");


        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register(nextCode, phone, getIMEI());
            }
        });
    }

    public void register(String nextCode, String phone, String daviceId) {
        String password_one = ed_password_one.getText().toString();
        String password_two = ed_password_two.getText().toString();
        if (StUtils.isEmpty(password_one)) {
            showToast("请输入密码");
            return;
        }

        if (StUtils.isEmpty(password_two)) {
            showToast("请输入密码");
            return;
        }
        if (!password_one.equals(password_two)) {
            showToast("两次输入密码不一致请修改");
            return;
        }
        if (!StUtils.isPasswordForm(password_one)) {
            showToast("密码须有字母和数字8位以上");
            return;
        }
        if (!StUtils.isPasswordForm(password_two)) {
            showToast("密码须有字母和数字8位以上");
            return;
        }


        post(Url.register, null, String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                if (response.contains("200")) {
                    login(phone, password_one, daviceId);
                }
            }

            @Override
            public boolean onFail(String response) {
                return false;
            }
        }, "{\"mobile\":\"" + phone + "\",\"nextCode\":\"" + nextCode + "\",\"password\":\"" +
                password_one + "\",\"deviceId\":\"" + daviceId + "\"}");
    }

    public void login(String phone, String password, String daviceId) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setMobile(phone);
        loginRequest.setPassword(password);
        loginRequest.setDeviceId(daviceId);
        loginRequest.setDeviceName(android.os.Build.MODEL);
        loginRequest.setLoginType("mobile_password");
        post(Url.login, "", LoginResponse.class, new FFNetWorkCallBack<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse response) {
                if (response.getCode() == 200) {
                    PreUtils.setString("token", response.getData().getToken());
                    PreUtils.setString("Authorization", response.getData().getTokenHead() + response.getData().getToken());
                    PreUtils.setString("phone", phone);
                    PreUtils.setBoolean("tourist", false);
                    PreUtils.setString("RefreshToken", response.getData().getRefreshToken());
                    Intent intent = new Intent();
                    intent.setClass(PasswordActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    showToast("密码不正确");
                }

            }

            @Override
            public boolean onFail(LoginResponse response) {
                showToast("网络出现问题");
                return false;
            }
        }, JSON.toJSONString(loginRequest));
    }

    @SuppressLint("HardwareIds")
    public String getIMEI() {
        String id;
//        //android.telephony.TelephonyManager
        TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            id = mTelephony.getDeviceId();
        } else {
            //android.provider.Settings;
            id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return id;
    }
}
